package com.domor.web.controller.demo.controller;

import com.domor.common.core.controller.BaseController;
import com.domor.common.utils.DateUtils;
import com.domor.web.controller.demo.domain.ExportData;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <h3>POI导出EXCEL demo</h3>
 * @author liyuyang
 * @date 2020/12/29
 */
@Controller
@RequestMapping("/data")
public class ExportExcelController extends BaseController {


    @RequestMapping("/exportExcel")
    public void exportExcel(Map<String, Object> params, HttpServletResponse response) {
        // 创建工作表
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("sheet1");
        Map<String, CellStyle> styles = ExportExcelUtils.createStyles(workbook);

        // 设置列宽
        sheet.setColumnWidth(0, 5000);
        sheet.setColumnWidth(1, 7000);
        sheet.setColumnWidth(2, 2000);
        sheet.setColumnWidth(3, 2000);
        sheet.setColumnWidth(4, 2000);
        sheet.setColumnWidth(5, 2000);
        sheet.setColumnWidth(6, 1500);
        sheet.setColumnWidth(7, 2000);
        sheet.setColumnWidth(8, 5000);

        // 标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("地区");
        row.createCell(1).setCellValue("监测点");
        row.createCell(2).setCellValue("类型");
        row.createCell(3).setCellValue("编号");
        row.createCell(4).setCellValue("累计流量");
        row.createCell(5).setCellValue("瞬时流量");
        row.createCell(6).setCellValue("温度");
        row.createCell(7).setCellValue("回灌率");
        row.createCell(8).setCellValue("采集时间");
        // 标题行样式
        for (int i = 0; i <= 8; i++) {
            row.getCell(i).setCellStyle(styles.get("header"));
        }

        // 输出内容
        List<ExportData> records = new ArrayList<>();
        int lastId = 0;
        for (int i = 0; i < records.size(); i++) {
            ExportData d = records.get(i);
            HSSFRow r = sheet.createRow(i + 1);
            r.createCell(0).setCellValue(d.getDeptName());
            r.createCell(1).setCellValue(d.getName());
            r.createCell(2).setCellValue(d.getType() == 1 ? "开采井" : "回灌井");
            r.createCell(3).setCellValue(d.getCode());
            r.createCell(4).setCellValue(d.getFlowrateSum());
            r.createCell(5).setCellValue((float) (Math.round(d.getFlowrateNow() * 100)) / 100);
            r.createCell(6).setCellValue(d.getTemperature());
            r.createCell(7).setCellValue(d.getRechargeRate() + "%");
            r.createCell(8).setCellValue(DateUtils.toTime(d.getDatetime()));
            // 内容行样式
            for (int j = 0; j <= 8; j++) {
                r.getCell(j).setCellStyle(styles.get("data"));
            }

            // 合并单元格
            if (d.getId() == lastId) {
                CellRangeAddress region0 = new CellRangeAddress(i, i + 1, 0, 0);
                CellRangeAddress region1 = new CellRangeAddress(i, i + 1, 1, 1);
                CellRangeAddress region7 = new CellRangeAddress(i, i + 1, 7, 7);
                CellRangeAddress region8 = new CellRangeAddress(i, i + 1, 8, 8);
                sheet.addMergedRegion(region0);
                sheet.addMergedRegion(region1);
                sheet.addMergedRegion(region7);
                sheet.addMergedRegion(region8);
            } else {
                lastId = d.getId();
            }
        }

        // 下载
        ExportExcelUtils.downLoadExcel(response, workbook, "实时数据.xlsx" );
    }

}
